package com.sb;

import java.util.concurrent.ConcurrentHashMap;

import net.neoremind.sshxcute.core.ConnBean;
import net.neoremind.sshxcute.core.Result;
import net.neoremind.sshxcute.core.SSHExec;
import net.neoremind.sshxcute.task.CustomTask;
import net.neoremind.sshxcute.task.impl.ExecCommand;

public class WorkerThread implements Runnable
{

	ConcurrentHashMap<String, ResultData> results;
	String hashKey;
	String[] mdsRow;
	private String username;
	private String password;

	public WorkerThread(ConcurrentHashMap<String, ResultData> results, String hashKey, String[] mdsRow, String username, String password)
	{
		this.results = results;
		this.hashKey = hashKey;
		this.mdsRow = mdsRow;
		this.username = username;
		this.password = password;
	}

	@Override
	public void run()
	{
		ResultData resultData = results.get(hashKey);

		// source must be in position 3
		// destination hostname must be at pos 6
		// port should be in position 9
		String hostname = mdsRow[2];
		String command = Messages.getString("FirewallRuleCheckUI.44") + mdsRow[5] + Messages.getString("FirewallRuleCheckUI.45") + mdsRow[8];

		try
		{
			ConnBean cb = new ConnBean(hostname, username, password);
			SSHExec ssh = SSHExec.getInstance(cb);
			// session.setConfig("PreferredAuthentications",
			// "publickey,keyboard-interactive,password");

			ssh.connect();
			CustomTask task;
			task = new ExecCommand(command);
			Result result = ssh.exec(task);
			result.display();
			if (result.sysout.indexOf(Messages.getString("FirewallRuleCheckUI.49")) > -1)
			{
				// SUCCESS
				// addMessageToStatus(rule +
				// Messages.getString("FirewallRuleCheckUI.50"));
				// outputRow.append(Messages.getString("FirewallRuleCheckUI.51"));
				resultData.passfail = true;
			}
			else
			{
				// FAILED
				// outputRow.append(Messages.getString("FirewallRuleCheckUI.52"));
				// addMessageToStatus(rule +
				// Messages.getString("FirewallRuleCheckUI.53"));
				if (result.syserr != null)
				{
					resultData.failReason = result.syserr;
				}
				resultData.passfail = false;
			}
		}
		catch (Exception e)
		{
			resultData.passfail = false;
			// System.out.println("Exception: "+e);

			if (e instanceof com.jcraft.jsch.JSchException)
			{
				if (e.getMessage() != null && resultData.failReason.length() == 0)
				{

					if (e.getMessage().indexOf(Messages.getString("FirewallRuleCheckUI.56")) > -1)
					{
						resultData.failReason = Messages.getString("FirewallRuleCheckUI.57");
					}
					else if (e.getMessage().indexOf(Messages.getString("FirewallRuleCheckUI.58")) > -1)
					{
						resultData.failReason = Messages.getString("FirewallRuleCheckUI.59");
					}
					else if (e.getMessage().indexOf("channel is not opened.") > -1)
					{
						resultData.failReason = Messages.getString("FirewallRuleCheckUI.59");
					}
					else if (e.getMessage().indexOf("java.lang.NullPointerException") > -1)
					{
						resultData.failReason = "Unknown-NullPointer";
						System.out.println("Exception: " + e);
					}
					else if (e.getMessage().indexOf("java.net.UnknownHostException") > -1)
					{
						resultData.failReason = Messages.getString("FirewallRuleCheckUI.55");
					}
					else if (e.getMessage().indexOf("java.net.SocketTimeoutException") > -1)
					{
						resultData.failReason = Messages.getString("FirewallRuleCheckUI.59");
					}
					else if (e.getMessage().indexOf("java.net.ConnectException") > -1)
					{
						resultData.failReason =e.getMessage();
					}
					else
					{
						resultData.failReason = e.getMessage();
						System.out.println("Unknown JSchException: " + e);
					}
				}// if we have a message and no failreason yet

			}// if JSchException
			else
			{
				resultData.failReason = e.getMessage();
				System.out.println("Unknown Exception: " + e);
			}
		}// catch
		finally
		{
			String rule = Messages.getString("FirewallRuleCheckUI.46") + hostname + Messages.getString("FirewallRuleCheckUI.47") + mdsRow[5] + Messages.getString("FirewallRuleCheckUI.48") + mdsRow[8]; //$NON-NLS-2$ //$NON-NLS-3$
			FirewallRuleCheckUI.addMessageToStatus(rule + " " + (resultData.passfail == true ? "[PASS]" : "[FAIL]") + " " + resultData.failReason);
			FirewallRuleCheckUI.incrementProgressBar();
		}

	}

}
